package com.monkey.tensquare.spit.service.impl;

import com.monkey.tensquare.spit.entity.Spit;
import com.monkey.tensquare.spit.mapper.SpitMapper;
import com.monkey.tensquare.spit.service.SpitService;
import entity.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import util.IdWorker;

import java.util.List;

@Service
public class SpitServiceImpl implements SpitService {
    @Autowired
    private IdWorker idWorker;
    @Autowired
    private SpitMapper spitMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public void add(Spit spit) {
        spit.set_id(idWorker.nextId() + "");
        spitMapper.insert(spit);
    }

    @Override
    public List<Spit> findAll() {
        return spitMapper.findAll();
    }

    @Override
    public Spit findById(String spitId) {
        return spitMapper.findById(spitId).get();
    }

    @Override
    public void update(String spitId, Spit spit) {
        spit.set_id(spitId);
        spitMapper.save(spit);
    }

    @Override
    public void delete(String spitId) {
        spitMapper.deleteById(spitId);
    }

    @Override
    public boolean thumbup(String spitId) {
        //根据id查询spit
        Spit spit = spitMapper.findById(spitId).get();
        //根据id查询是否点赞,需要确定当前用户是否点赞，根据用户id和评论id
        Object isThumbup = redisTemplate.opsForValue().get("userid" + spitId);
        if (isThumbup != null){
            return false;
        }
        //修改点赞数
        spit.setThumbup(spit.getThumbup() + 1);
        //修改redis,修改mongodb
        redisTemplate.opsForValue().set("userid" + spitId, "isThumbup");
        spitMapper.save(spit);
        return true;
    }

    @Override
    public PageResult<Spit> comment(String parentid, Integer page, Integer size) {
        Page<Spit> spitPage = spitMapper.findByParentid(parentid, PageRequest.of(page - 1, size));
        return new PageResult<>(spitPage.getTotalElements(),spitPage.getContent());
    }

    @Override
    public PageResult<Spit> search(Spit spit, Integer page, Integer size) {
        System.out.println("条件查询带分页！...设置字段content模糊查询");
        //构造条件匹配器
        //忽略大小写，设置模糊查询，设置模糊查询字段
        //matchingAll:表示全部字段都能匹配上（等值匹配），才查出
        //matchingAny:表示，任何一个字段匹配上（等值匹配），都查出
        //matching:表示自定义字段匹配，可以设置自定义匹配规则
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withIgnoreCase(true)
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withMatcher("content",ExampleMatcher.GenericPropertyMatchers.contains());
        //添加条件
        Example<Spit> example = Example.of(spit,matcher);
        //根据条件分页查询
        Page<Spit> spitPage = spitMapper.findAll(example, PageRequest.of(page - 1, size));
        return new PageResult<>(spitPage.getTotalElements(),spitPage.getContent());
    }
}
